package com.gimi.cloud.jimi.task.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gimi.cloud.jimi.common.enums.common.RedisKeyEnum;
import com.gimi.cloud.jimi.common.utils.DateUtil;
import com.gimi.cloud.jimi.commonService.InterfacePlatform.InterfacePlatformRequestService;
import com.gimi.cloud.jimi.dao.dao.moreInfo.FengchaoGongjijinRegionDao;
import com.gimi.cloud.jimi.dao.redis.IRedisService;
import com.gimi.cloud.jimi.entity.app.moreInfo.FengchaoGongjijinRegionEntity;
import com.gimi.cloud.jimi.task.service.ProvidentFundService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @auther loongs
 * @date 2018/1/10  13:18
 */
@Service
public class ProvidentFundServiceImpl implements ProvidentFundService {

    private static Logger log = LoggerFactory.getLogger(ProvidentFundServiceImpl.class);

    private IRedisService iRedisService;
    private InterfacePlatformRequestService requestService;
    private FengchaoGongjijinRegionDao fengchaoGongjijinRegionDao;

    @Autowired
    public ProvidentFundServiceImpl(IRedisService iRedisService, InterfacePlatformRequestService requestService
            , FengchaoGongjijinRegionDao fengchaoGongjijinRegionDao) {
        this.iRedisService = iRedisService;
        this.requestService = requestService;
        this.fengchaoGongjijinRegionDao = fengchaoGongjijinRegionDao;
    }

    @Override
    public void getAccumulationFundSupportedCityList() {
        log.info("开始获取蜂巢支持获取公积金的城市列表");
        log.info("获取请求参数");
        String fengchao_gongjijing = iRedisService.get(RedisKeyEnum.JIMI_CONFIG_PRE.getKey() + "interface_platform_universal_getProvidentFundInfo");
        JSONObject fgJson = JSON.parseObject(fengchao_gongjijing);
        String locationlist = fgJson.getString("fengchao_gongjijin_getlocationlist");
        JSONObject rsparam = fgJson.getJSONObject("fengchao_gongjijin_getregionsigninparameter");
        JSONArray data = null;

        log.info("发送请求并解析参数");
        try {
            String sendPostJson = requestService.sendApiPost(locationlist);
            data = JSON.parseObject(sendPostJson).getJSONObject("body").getJSONArray("data");
        } catch (Exception e) {
            log.error("获取蜂巢支持获取公积金的城市列表异常：" + e);
            e.printStackTrace();
            return;
        }
        log.info("遍历城市列表获取城市对应的访问参数并保存");
        for (int i = 0; i < data.size(); i++) {
            try {
                JSONObject jsonObject = data.getJSONObject(i);
                String regionId = jsonObject.getString("regionId");
                String regionName = jsonObject.getString("regionName");
                log.info("开始获取" + regionName + "的访问参数");
                Integer regionStatus = jsonObject.getInteger("regionStatus");
                JSONObject reqBody = new JSONObject();
                reqBody.put("location_cid", regionId);
                rsparam.put("body", reqBody);
                String rspRespon = requestService.sendApiPost(rsparam.toJSONString());
                FengchaoGongjijinRegionEntity feEntity = null;
                feEntity = fengchaoGongjijinRegionDao.selectFcgjjrByCityId(regionId);
                if (feEntity == null) {
                    feEntity = new FengchaoGongjijinRegionEntity();
                    feEntity.setRegionCityId(regionId);
                    feEntity.setRegionCityName(regionName);
                    feEntity.setRegionStatus(regionStatus);
                    feEntity.setAddTime(DateUtil.getNow());
                }
                JSONObject repBody = JSON.parseObject(rspRespon).getJSONObject("body");
                feEntity.setRegionArg(repBody.toJSONString());
                if (feEntity.getRegionId() == null) {
                    fengchaoGongjijinRegionDao.insertSelective(feEntity);
                } else {
                    feEntity.setUpdateTime(DateUtil.getNow());
                    fengchaoGongjijinRegionDao.updateByPrimaryKey(feEntity);
                }
            } catch (Exception e) {
                log.error("获取地区登录参数异常：" + e);
                e.printStackTrace();
                continue;
            }
        }
        log.info("获取蜂巢支持获取公积金的城市列表结束");
    }
}
